package com.kuang.sync.singleup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuang.mapper.*;
import com.kuang.pojo.*;
import com.kuang.sync.singledown.SingleDownService;
import com.kuang.utils.MQTTConnect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class SingleUpServiceImpl implements SingleUpService {

    //设备端心跳
    @Resource
    AstControllerHeartMapper astControllerHeartMapper;
    //设备端注册
    @Resource
    AstControllerRegisterMapper astControllerRegisterMapper;
    //设备端开机
    @Resource
    AstControllerPoweronMapper astControllerPoweronMapper;
    //设备端本地开井
    @Resource
    AstLocalOpenWellMapper astLocalOpenWellMapper;
    //设备端本地关井
    @Resource
    AstLocalCloseWellMapper astLocalCloseWellMapper;
    //设备端本地开井实时数据 cmd=local_opening_well
    @Resource
    AstLocalOpeningWellMapper astLocalOpeningWellMapper;
    @Resource
    AstRemoteOpeningWellMapper astRemoteOpeningWellMapper;
    @Resource
    MQTTConnect mqttConnect;
    @Resource
    SingleDownService singleDownService;

    /**
     * 1.设备端心跳 cmd=controller_heart
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String controllerHeartSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstControllerHeart astControllerHeart = JSON.parseObject(JSON.toJSONString(jsonObject), AstControllerHeart.class);
        ;
        //设备编码
        astControllerHeart.setDevice_code(id);
        //主键
        astControllerHeart.setAst_controller_heart_id(String.valueOf(UUID.randomUUID()));
        astControllerHeart.setCreate_time(new Date());
        astControllerHeart.setCmd(cmd);
        astControllerHeart.setVer(ver);

        int insert = astControllerHeartMapper.insert(astControllerHeart);

        JSONObject outJson = new JSONObject();
        JSONObject data = new JSONObject();
        outJson.put("cmd", 1);
        outJson.put("ver", ver);
        data.put("state", "1");
        outJson.put("data", data);
        String outJsonS = JSONObject.toJSONString(outJson);
        try {
            mqttConnect.pub("/single/down/" + id, outJsonS);//发布主题
            log.info("发送给 " + "/single/down/" + id + " 的消息：{}", outJsonS);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        //singleDownService.postControllerHeart(astControllerHeart);

        return null;
    }

    /**
     * 2.设备端注册 cmd=controller_register
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String controllerRegisterSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstControllerRegister astControllerRegister = JSON.parseObject(JSON.toJSONString(jsonObject), AstControllerRegister.class);
        //设备编码
        astControllerRegister.setDevice_code(id);
        //主键
        astControllerRegister.setAst_controller_register_id(String.valueOf(UUID.randomUUID()));
        astControllerRegister.setCreate_time(new Date());
        astControllerRegister.setCmd(cmd);
        astControllerRegister.setVer(ver);

        int insert = astControllerRegisterMapper.insert(astControllerRegister);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        try {
            String s = singleDownService.postControllerRegisterSrv(id, preant);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 3. 设备端开机 cmd=controller_poweron
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String controllerPoweronSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstControllerPoweron astControllerPoweron = JSON.parseObject(JSON.toJSONString(jsonObject), AstControllerPoweron.class);
        //设备编码
        astControllerPoweron.setDevice_code(id);
        //主键
        astControllerPoweron.setAst_controller_poweron_id(String.valueOf(UUID.randomUUID()));
        astControllerPoweron.setCreate_time(new Date());
        astControllerPoweron.setCmd(cmd);
        astControllerPoweron.setVer(ver);

        int insert = astControllerPoweronMapper.insert(astControllerPoweron);

        //返回给设备信息
        JSONObject outJson = new JSONObject();
        JSONObject data = new JSONObject();
        outJson.put("cmd", 3);
        outJson.put("ver", ver);
        data.put("state", "1");
        data.put("dev_register_no", astControllerPoweron.getDev_register_no());
        outJson.put("data", data);
        String outJsonS = JSONObject.toJSONString(outJson);
        try {
            mqttConnect.pub("/single/down/" + id, outJsonS);//发布主题
            log.info("发送给 " + "/single/down/" + id + " 的消息：{}", outJsonS);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        singleDownService.postControllerPoweron(astControllerPoweron);

        return null;
    }


    /**
     * 4.设备端本地开井 cmd=local_open_well
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String localOpenWellSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstLocalOpenWell astLocalOpenWell = JSON.parseObject(JSON.toJSONString(jsonObject), AstLocalOpenWell.class);
        //设备编码
        astLocalOpenWell.setDevice_code(id);
        //主键
        astLocalOpenWell.setAst_local_open_well_id(String.valueOf(UUID.randomUUID()));
        Date now = new Date();
        astLocalOpenWell.setCreate_time(now);
        astLocalOpenWell.setCmd(cmd);
        astLocalOpenWell.setVer(ver);

        //begin     添加时间 2023/4/2张玉龙，蒋修裕，添加字段 open_time     --zoe_zls
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String open_time = jsonObject.getString("open_time");
        if (StringUtils.isNotBlank(open_time)){
            try {
                Date date = simpleDateFormat.parse(open_time);
                astLocalOpenWell.setOpen_time(date);
            } catch (ParseException e) {
                astLocalOpenWell.setOpen_time(now);
                e.printStackTrace();
            }
        }
        //end;

        int insert = astLocalOpenWellMapper.insert(astLocalOpenWell);

        //返回给设备信息
        JSONObject outJson = new JSONObject();
        JSONObject data = new JSONObject();
        outJson.put("cmd", 4);
        outJson.put("ver", ver);
        data.put("state", "1");
        outJson.put("data", data);
        String outJsonS = JSONObject.toJSONString(outJson);
        try {
            mqttConnect.pub("/single/down/" + id, outJsonS);//发布主题
            log.info("发送给 " + "/single/down/" + id + " 的消息：{}", outJsonS);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        singleDownService.postLocalOpen(astLocalOpenWell);

        return null;
    }


    /**
     * 5.设备端本地关井 cmd=local_close_well
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String localCloseWellSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstLocalCloseWell astLocalCloseWell = JSON.parseObject(JSON.toJSONString(jsonObject), AstLocalCloseWell.class);
        //设备编码
        astLocalCloseWell.setDevice_code(id);
        //主键
        Date now = new Date();
        astLocalCloseWell.setAst_local_close_well_id(String.valueOf(UUID.randomUUID()));
        astLocalCloseWell.setCreate_time(now);
        astLocalCloseWell.setCmd(cmd);
        astLocalCloseWell.setVer(ver);

        //begin     添加时间 2023/4/2张玉龙，蒋修裕，添加字段 close_time     --zoe_zls
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String close_time = jsonObject.getString("close_time");
        if (StringUtils.isNotBlank(close_time)){
            try {
                Date date = simpleDateFormat.parse(close_time);
                astLocalCloseWell.setClose_time(date);
            } catch (ParseException e) {
                astLocalCloseWell.setClose_time(now);
                e.printStackTrace();
            }
        }
        //end;

        int insert = astLocalCloseWellMapper.insert(astLocalCloseWell);

        //返回给设备信息
        JSONObject outJson = new JSONObject();
        JSONObject data = new JSONObject();
        outJson.put("cmd", 5);
        outJson.put("ver", ver);
        data.put("state", "1");
        outJson.put("data", data);
        String outJsonS = JSONObject.toJSONString(outJson);
        try {
            mqttConnect.pub("/single/down/" + id, outJsonS);//发布主题
            log.info("发送给 " + "/single/down/" + id + " 的消息：{}", outJsonS);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        singleDownService.postLocalClose(astLocalCloseWell);

        return null;
    }

    /**
     * 8. 设备端本地开井实时数据 cmd=local_opening_well
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String localOpeningWellSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstLocalOpeningWell astLocalOpeningWell = JSON.parseObject(JSON.toJSONString(jsonObject), AstLocalOpeningWell.class);
        //设备编码
        astLocalOpeningWell.setDevice_code(id);
        //主键
        astLocalOpeningWell.setAst_local_opening_well_id(String.valueOf(UUID.randomUUID()));
        astLocalOpeningWell.setCreate_time(new Date());
        astLocalOpeningWell.setCmd(cmd);
        astLocalOpeningWell.setVer(ver);

        int insert = astLocalOpeningWellMapper.insert(astLocalOpeningWell);

        //返回给设备信息
        JSONObject outJson = new JSONObject();
        JSONObject data = new JSONObject();
        outJson.put("cmd", 8);
        outJson.put("ver", ver);
        data.put("state", "1");
        outJson.put("data", data);
        String outJsonS = JSONObject.toJSONString(outJson);
        try {
            mqttConnect.pub("/single/down/" + id, outJsonS);//发布主题
            log.info("发送给 " + "/single/down/" + id + " 的消息：{}", outJsonS);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        singleDownService.postRealTimeRecords("local",astLocalOpeningWell);

        return null;
    }

    /**
     * 9.设备端远程开井实时数据 cmd=remote_opening_well
     *
     * @param id
     * @param json
     * @return
     */
    @Override
    public String remoteOpeningWellSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstRemoteOpeningWell astRemoteOpeningWell = JSON.parseObject(JSON.toJSONString(jsonObject), AstRemoteOpeningWell.class);
        //设备编码
        astRemoteOpeningWell.setDevice_code(id);
        //主键
        astRemoteOpeningWell.setAst_remote_opening_well_id(String.valueOf(UUID.randomUUID()));
        astRemoteOpeningWell.setCreate_time(new Date());
        astRemoteOpeningWell.setCmd(cmd);
        astRemoteOpeningWell.setVer(ver);

        int insert = astRemoteOpeningWellMapper.insert(astRemoteOpeningWell);

        //返回给设备信息
        JSONObject outJson = new JSONObject();
        JSONObject data = new JSONObject();
        outJson.put("cmd", 9);
        outJson.put("ver", ver);
        data.put("state", "1");
        outJson.put("data", data);
        String outJsonS = JSONObject.toJSONString(outJson);
        try {
            mqttConnect.pub("/single/down/" + id, outJsonS);//发布主题
            log.info("发送给 " + "/single/down/" + id + " 的消息：{}", outJsonS);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        singleDownService.postRealTimeRecords("remote",astRemoteOpeningWell);

        return null;
    }

}
